Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add regression test for multiple engine reloads #704

Merged
merged 7 commits into from
Aug 18, 2022

Conversation

mjcarroll
Copy link
Contributor

@mjcarroll mjcarroll commented Aug 11, 2022

🦟 Bug fix

Summary

Similar to previous crashes we have had, we weren't appropriately cleaning up resources in our rendering objects.

I uncovered this while testing gz-sensors7, as it wasn't revealed by tests here.

As a result, I have added a regression test that will attempt to load and unload a rendering engine multiple times while also instantiating sensors. If everything is cleaned up correctly, this test runs without any segfaults.

A have also included fixes for the DepthCamera and GpuRays sensor that were detected via this test.

Checklist

  • Signed all commits for DCO
  • Added tests
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • Consider updating Python bindings (if the library has them)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by messages.

@mjcarroll mjcarroll requested a review from iche033 as a code owner August 11, 2022 21:51
@mjcarroll
Copy link
Contributor Author

I also removed a few members that I discovered were unused in my debugging.

@github-actions github-actions bot added the 🌱 garden Ignition Garden label Aug 11, 2022
@chapulina chapulina added the bug Something isn't working label Aug 11, 2022
@mjcarroll mjcarroll changed the title Fix removing viewport in Ogre DepthCamera Add regression test for multiple engine reloads Aug 12, 2022
@mjcarroll mjcarroll force-pushed the mjcarroll/ogre_depth_camera_segfault branch from f604212 to 88421a4 Compare August 12, 2022 02:05
Signed-off-by: Michael Carroll <[email protected]>
@mjcarroll
Copy link
Contributor Author

I have been unable to reproduce INTEGRATION_depth_camera_ogre_gl3plus locally, but it seems to fail consistently across our platforms. I will try it on macos and see what happens.

@mjcarroll
Copy link
Contributor Author

@darksylinc There seems to be something specifically wrong with the OGRE 1 depth camera. I have valgrind/gdb for a bit and I'm kind of stumped. I think it's a viewport not getting disconnected correctly?

If you have a quick guess I can try to track down, it would be appreciated.

EXPORT GZ_ENGINE=ogre
EXPORT GZ_ENGINE_BACKEND=gl3plus
gdb --args ./bin/INTEGRATION_depth_camera --gtest_filter="*DepthCameraBoxes"
#0  __pthread_kill_implementation (no_tid=0, signo=6, threadid=140737320557760) at ./nptl/pthread_kill.c:44
#1  __pthread_kill_internal (signo=6, threadid=140737320557760) at ./nptl/pthread_kill.c:78
#2  __GI___pthread_kill (threadid=140737320557760, signo=signo@entry=6) at ./nptl/pthread_kill.c:89
#3  0x00007ffff7902476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#4  0x00007ffff78e87f3 in __GI_abort () at ./stdlib/abort.c:79
#5  0x00007ffff79496f6 in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff7a9bb8c "%s\n") at ../sysdeps/posix/libc_fatal.c:155
#6  0x00007ffff7960d7c in malloc_printerr (str=str@entry=0x7ffff7a9e5d8 "malloc_consolidate(): unaligned fastbin chunk detected") at ./malloc/malloc.c:5664
#7  0x00007ffff7961a0c in malloc_consolidate (av=av@entry=0x7ffff7ad9c80 <main_arena>) at ./malloc/malloc.c:4750
#8  0x00007ffff7962f20 in _int_free (av=0x7ffff7ad9c80 <main_arena>, p=0x5555565bb1b0, have_lock=<optimized out>) at ./malloc/malloc.c:4674
#9  0x00007ffff79654d3 in __GI___libc_free (mem=<optimized out>) at ./malloc/malloc.c:3391
#10 0x00007ffff2dda6e5 in ?? () from /lib/x86_64-linux-gnu/libnvidia-glcore.so.470.141.03
#11 0x00007ffff2abdac8 in ?? () from /lib/x86_64-linux-gnu/libnvidia-glcore.so.470.141.03
#12 0x00007ffff2f094dc in ?? () from /lib/x86_64-linux-gnu/libnvidia-glcore.so.470.141.03
#13 0x00007ffff2bb05b0 in ?? () from /lib/x86_64-linux-gnu/libnvidia-glcore.so.470.141.03
#14 0x00007ffff2f0af78 in ?? () from /lib/x86_64-linux-gnu/libnvidia-glcore.so.470.141.03
#15 0x00007ffff2b79113 in ?? () from /lib/x86_64-linux-gnu/libnvidia-glcore.so.470.141.03
#16 0x00007ffff1804b4d in Ogre::GLTexture::freeInternalResourcesImpl() () from /usr/lib/x86_64-linux-gnu/OGRE-1.9.0/RenderSystem_GL.so
#17 0x00007ffff5abc11d in Ogre::Texture::unloadImpl() () from /lib/x86_64-linux-gnu/libOgreMain.so.1.9.0
#18 0x00007ffff5a00c48 in Ogre::Resource::unload() () from /lib/x86_64-linux-gnu/libOgreMain.so.1.9.0
#19 0x00007ffff17fefed in Ogre::GLTexture::~GLTexture() () from /usr/lib/x86_64-linux-gnu/OGRE-1.9.0/RenderSystem_GL.so
#20 0x00007ffff17ff06d in Ogre::GLTexture::~GLTexture() () from /usr/lib/x86_64-linux-gnu/OGRE-1.9.0/RenderSystem_GL.so
#21 0x00007ffff58de3a9 in ?? () from /lib/x86_64-linux-gnu/libOgreMain.so.1.9.0
#22 0x00007ffff5a8da81 in Ogre::ShadowTextureManager::clearUnused() () from /lib/x86_64-linux-gnu/libOgreMain.so.1.9.0
#23 0x00007ffff5a41da1 in Ogre::SceneManager::destroyShadowTextures() () from /lib/x86_64-linux-gnu/libOgreMain.so.1.9.0
#24 0x00007ffff5a269a7 in Ogre::SceneManager::~SceneManager() () from /lib/x86_64-linux-gnu/libOgreMain.so.1.9.0
#25 0x00007ffff13d210b in ?? () from /usr/lib/x86_64-linux-gnu/OGRE-1.9.0/Plugin_OctreeSceneManager.so
#26 0x00007ffff5a4c860 in Ogre::SceneManagerEnumerator::removeFactory(Ogre::SceneManagerFactory*) () from /lib/x86_64-linux-gnu/libOgreMain.so.1.9.0
#27 0x00007ffff5a21e0e in Ogre::Root::shutdownPlugins() () from /lib/x86_64-linux-gnu/libOgreMain.so.1.9.0
#28 0x00007ffff5a1d21a in Ogre::Root::shutdown() () from /lib/x86_64-linux-gnu/libOgreMain.so.1.9.0

Valgrind seems a bit more interesting:

==1799311== Invalid write of size 1
==1799311==    at 0x4B46E4D: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.30)
==1799311==    by 0x6E470B1: assign (basic_string.h:1387)
==1799311==    by 0x6E470B1: operator= (basic_string.h:681)
==1799311==    by 0x6E470B1: setMaterialScheme (OgreViewport.h:276)
==1799311==    by 0x6E470B1: gz::rendering::v7::OgreRTShaderSystem::DetachViewport(Ogre::Viewport*, std::shared_ptr<gz::rendering::v7::OgreScene>) (OgreRTShaderSystem.cc:287)
==1799311==    by 0x6E58FF7: gz::rendering::v7::OgreRenderTarget::~OgreRenderTarget() (OgreRenderTarget.cc:64)
==1799311==    by 0x6E598BE: gz::rendering::v7::OgreRenderTexture::~OgreRenderTexture() (OgreRenderTarget.cc:310)
==1799311==    by 0x6E5994C: gz::rendering::v7::OgreRenderTexture::~OgreRenderTexture() (OgreRenderTarget.cc:310)
==1799311==    by 0x124321: std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() (shared_ptr_base.h:168)
==1799311==    by 0x6DD88BE: ~__shared_count (shared_ptr_base.h:705)
==1799311==    by 0x6DD88BE: ~__shared_ptr (shared_ptr_base.h:1154)
==1799311==    by 0x6DD88BE: ~shared_ptr (shared_ptr.h:122)
==1799311==    by 0x6DD88BE: ~OgreDepthCameraPrivate (OgreDepthCamera.cc:31)
==1799311==    by 0x6DD88BE: operator() (unique_ptr.h:85)
==1799311==    by 0x6DD88BE: operator() (unique_ptr.h:79)
==1799311==    by 0x6DD88BE: ~unique_ptr (unique_ptr.h:361)
==1799311==    by 0x6DD88BE: gz::rendering::v7::OgreDepthCamera::~OgreDepthCamera() (OgreDepthCamera.cc:87)
==1799311==    by 0x6DD8A5C: gz::rendering::v7::OgreDepthCamera::~OgreDepthCamera() (OgreDepthCamera.cc:87)
==1799311==    by 0x6DB8BBF: _M_release (shared_ptr_base.h:168)
==1799311==    by 0x6DB8BBF: ~__shared_count (shared_ptr_base.h:705)
==1799311==    by 0x6DB8BBF: ~__shared_ptr (shared_ptr_base.h:1154)
==1799311==    by 0x6DB8BBF: ~shared_ptr (shared_ptr.h:122)
==1799311==    by 0x6DB8BBF: gz::rendering::v7::BaseNode<gz::rendering::v7::OgreObject>::RemoveChildren() (BaseNode.hh:302)
==1799311==    by 0x48B9805: gz::rendering::v7::BaseScene::Clear() (BaseScene.cc:1423)
==1799311==    by 0x48AF6B0: gz::rendering::v7::BaseScene::Destroy() (BaseScene.cc:1434)
==1799311==    by 0x6E5D62C: gz::rendering::v7::OgreScene::Destroy() (OgreScene.cc:300)
==1799311==  Address 0x6b7aef5 is 5 bytes inside a block of size 34 free'd
==1799311==    at 0x484BB6F: operator delete(void*, unsigned long) (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==1799311==    by 0x734F21C: Ogre::Viewport::~Viewport() (in /usr/lib/x86_64-linux-gnu/libOgreMain.so.1.9.0)
==1799311==    by 0x7279354: Ogre::RenderTarget::removeAllViewports() (in /usr/lib/x86_64-linux-gnu/libOgreMain.so.1.9.0)
==1799311==    by 0x6DD841E: gz::rendering::v7::OgreDepthCamera::Destroy() (OgreDepthCamera.cc:117)
==1799311==    by 0x6E2F937: gz::rendering::v7::BaseStore<gz::rendering::v7::Sensor, gz::rendering::v7::OgreSensor>::DestroyImpl(std::_Rb_tree_iterator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::shared_ptr<gz::rendering::v7::OgreSensor> > >) (BaseStorage.hh:956)
==1799311==    by 0x6E29B39: gz::rendering::v7::BaseStore<gz::rendering::v7::Sensor, gz::rendering::v7::OgreSensor>::DestroyAll() (BaseStorage.hh:734)
==1799311==    by 0x48BF0BD: gz::rendering::v7::BaseCompositeStore<gz::rendering::v7::Node>::DestroyAll() (BaseStorage.hh:1216)
==1799311==    by 0x48B97D3: DestroyNodes (BaseScene.cc:333)
==1799311==    by 0x48B97D3: gz::rendering::v7::BaseScene::Clear() (BaseScene.cc:1419)
==1799311==    by 0x48AF6B0: gz::rendering::v7::BaseScene::Destroy() (BaseScene.cc:1434)
==1799311==    by 0x6E5D62C: gz::rendering::v7::OgreScene::Destroy() (OgreScene.cc:300)
==1799311==    by 0x6E2E890: gz::rendering::v7::BaseStore<gz::rendering::v7::Scene, gz::rendering::v7::OgreScene>::DestroyImpl(std::_Rb_tree_iterator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::shared_ptr<gz::rendering::v7::OgreScene> > >) (BaseStorage.hh:956)
==1799311==    by 0x48ADCEF: gz::rendering::v7::BaseRenderEngine::DestroyScene(std::shared_ptr<gz::rendering::v7::Scene>) (BaseRenderEngine.cc:163)

@mjcarroll
Copy link
Contributor Author

Actually, this is a better valgrind trace (without some of my modifications/checks)

==1807158== Invalid write of size 2
==1807158==    at 0x48529E3: memmove (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==1807158==    by 0x4B46EAE: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.30)
==1807158==    by 0x6E470B1: assign (basic_string.h:1387)
==1807158==    by 0x6E470B1: operator= (basic_string.h:681)
==1807158==    by 0x6E470B1: setMaterialScheme (OgreViewport.h:276)
==1807158==    by 0x6E470B1: gz::rendering::v7::OgreRTShaderSystem::DetachViewport(Ogre::Viewport*, std::shared_ptr<gz::rendering::v7::OgreScene>) (OgreRTShaderSystem.cc:287)
==1807158==    by 0x6E58FA7: gz::rendering::v7::OgreRenderTarget::~OgreRenderTarget() (OgreRenderTarget.cc:66)
==1807158==    by 0x6E5986E: gz::rendering::v7::OgreRenderTexture::~OgreRenderTexture() (OgreRenderTarget.cc:312)
==1807158==    by 0x6E598FC: gz::rendering::v7::OgreRenderTexture::~OgreRenderTexture() (OgreRenderTarget.cc:312)
==1807158==    by 0x124321: std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() (shared_ptr_base.h:168)
==1807158==    by 0x6DD88BE: ~__shared_count (shared_ptr_base.h:705)
==1807158==    by 0x6DD88BE: ~__shared_ptr (shared_ptr_base.h:1154)
==1807158==    by 0x6DD88BE: ~shared_ptr (shared_ptr.h:122)
==1807158==    by 0x6DD88BE: ~OgreDepthCameraPrivate (OgreDepthCamera.cc:31)
==1807158==    by 0x6DD88BE: operator() (unique_ptr.h:85)
==1807158==    by 0x6DD88BE: operator() (unique_ptr.h:79)
==1807158==    by 0x6DD88BE: ~unique_ptr (unique_ptr.h:361)
==1807158==    by 0x6DD88BE: gz::rendering::v7::OgreDepthCamera::~OgreDepthCamera() (OgreDepthCamera.cc:87)
==1807158==    by 0x6DD8A5C: gz::rendering::v7::OgreDepthCamera::~OgreDepthCamera() (OgreDepthCamera.cc:87)
==1807158==    by 0x6DB8BBF: _M_release (shared_ptr_base.h:168)
==1807158==    by 0x6DB8BBF: ~__shared_count (shared_ptr_base.h:705)
==1807158==    by 0x6DB8BBF: ~__shared_ptr (shared_ptr_base.h:1154)
==1807158==    by 0x6DB8BBF: ~shared_ptr (shared_ptr.h:122)
==1807158==    by 0x6DB8BBF: gz::rendering::v7::BaseNode<gz::rendering::v7::OgreObject>::RemoveChildren() (BaseNode.hh:302)
==1807158==    by 0x48B9805: gz::rendering::v7::BaseScene::Clear() (BaseScene.cc:1423)
==1807158==    by 0x48AF6B0: gz::rendering::v7::BaseScene::Destroy() (BaseScene.cc:1434)
==1807158==  Address 0xc697c60 is 0 bytes inside a block of size 34 free'd
==1807158==    at 0x484BB6F: operator delete(void*, unsigned long) (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==1807158==    by 0x734F21C: Ogre::Viewport::~Viewport() (in /usr/lib/x86_64-linux-gnu/libOgreMain.so.1.9.0)
==1807158==    by 0x7279354: Ogre::RenderTarget::removeAllViewports() (in /usr/lib/x86_64-linux-gnu/libOgreMain.so.1.9.0)
==1807158==    by 0x6DD841E: gz::rendering::v7::OgreDepthCamera::Destroy() (OgreDepthCamera.cc:117)
==1807158==    by 0x6E2F937: gz::rendering::v7::BaseStore<gz::rendering::v7::Sensor, gz::rendering::v7::OgreSensor>::DestroyImpl(std::_Rb_tree_iterator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::shared_ptr<gz::rendering::v7::OgreSensor> > >) (BaseStorage.hh:956)
==1807158==    by 0x6E29B39: gz::rendering::v7::BaseStore<gz::rendering::v7::Sensor, gz::rendering::v7::OgreSensor>::DestroyAll() (BaseStorage.hh:734)
==1807158==    by 0x48BF0BD: gz::rendering::v7::BaseCompositeStore<gz::rendering::v7::Node>::DestroyAll() (BaseStorage.hh:1216)
==1807158==    by 0x48B97D3: DestroyNodes (BaseScene.cc:333)
==1807158==    by 0x48B97D3: gz::rendering::v7::BaseScene::Clear() (BaseScene.cc:1419)
==1807158==    by 0x48AF6B0: gz::rendering::v7::BaseScene::Destroy() (BaseScene.cc:1434)
==1807158==    by 0x6E5D5DC: gz::rendering::v7::OgreScene::Destroy() (OgreScene.cc:300)
==1807158==    by 0x6E2E890: gz::rendering::v7::BaseStore<gz::rendering::v7::Scene, gz::rendering::v7::OgreScene>::DestroyImpl(std::_Rb_tree_iterator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::shared_ptr<gz::rendering::v7::OgreScene> > >) (BaseStorage.hh:956)
==1807158==    by 0x48ADCEF: gz::rendering::v7::BaseRenderEngine::DestroyScene(std::shared_ptr<gz::rendering::v7::Scene>) (BaseRenderEngine.cc:163)

@darksylinc
Copy link
Contributor

Didn't I submit a PR to fix similar issues recently? I can't find it!

@darksylinc
Copy link
Contributor

Ahhh it wasn't a PR

It was a patch: #679 (comment)

I suspect the other problems have similar solutions.

@darksylinc
Copy link
Contributor

darksylinc commented Aug 12, 2022

To be more specific this part was super important:

  if (this->dataPtr->thermalInstance)
  {
    // Do not leave a reference to this->dataPtr->thermalMaterial
    Ogre::MaterialPtr nullMaterial;
    this->dataPtr->thermalInstance->getTechnique()
      ->getOutputTargetPass()
      ->getPass(0)
      ->setMaterial(nullMaterial);
  }

Because otherwise a texture would linger in a material that still exists. We need to unset the texture from that material. That material was used by the compositor.

@darksylinc
Copy link
Contributor

@mjcarroll
Copy link
Contributor Author

To repro all this I need to checkout https://github.com/gazebosim/gz-rendering/tree/mjcarroll/ogre_depth_camera_segfault ?

That is all that is necessary. I attempted to duplicate the patch from the thermal and wide angle cameras, but this one doesn't use the same structure to the texture/material/compositor

@darksylinc
Copy link
Contributor

darksylinc commented Aug 12, 2022

Valgrind is indeed finding something wrong on Gazebo side.

~OgreRenderTarget() calls DetachViewport but this has already been done in OgreRenderTexture::DestroyTarget and there are no checks either (that the Viewport is a valid ptr).

This stray DetachViewport call sounds like an accidental left over from either a refactor or a git merge. I'm still looking into it.

@darksylinc
Copy link
Contributor

darksylinc commented Aug 12, 2022

This patch fixes INTEGRATION_depth_camera:

diff --git a/ogre/src/OgreDepthCamera.cc b/ogre/src/OgreDepthCamera.cc
index 0dd31cce..b6139696 100644
--- a/ogre/src/OgreDepthCamera.cc
+++ b/ogre/src/OgreDepthCamera.cc
@@ -109,12 +109,14 @@ void OgreDepthCamera::Destroy()
 
   if (this->dataPtr->pcdTexture)
   {
-    this->dataPtr->pcdTexture->RenderTarget()->removeAllViewports();
+    this->dataPtr->pcdTexture->Destroy();
+    this->dataPtr->pcdTexture.reset();
   }
 
   if (this->dataPtr->colorTexture)
   {
-    this->dataPtr->colorTexture->RenderTarget()->removeAllViewports();
+    this->dataPtr->colorTexture->Destroy();
+    this->dataPtr->colorTexture.reset();
   }
 
   if (!this->ogreCamera || !this->scene->IsInitialized())
diff --git a/ogre/src/OgreRenderTarget.cc b/ogre/src/OgreRenderTarget.cc
index d7ed64af..c1f02a81 100644
--- a/ogre/src/OgreRenderTarget.cc
+++ b/ogre/src/OgreRenderTarget.cc
@@ -63,8 +63,7 @@ OgreRenderTarget::~OgreRenderTarget()
 {
   // TODO(anyone): clean up check null
 
-  OgreRTShaderSystem::Instance()->DetachViewport(this->ogreViewport,
-      this->scene);
+  GZ_ASSERT(this->ogreViewport == nullptr, "DestroyTarget not called!");
 }
 
 //////////////////////////////////////////////////
@@ -339,6 +338,8 @@ void OgreRenderTexture::DestroyTarget()
   if (nullptr == this->ogreTexture)
     return;
 
+  this->materialApplicator.reset();
+
   OgreRTShaderSystem::Instance()->DetachViewport(this->ogreViewport,
       this->scene);
 
@@ -351,6 +352,7 @@ void OgreRenderTexture::DestroyTarget()
   auto engine = OgreRenderEngine::Instance();
   engine->OgreRoot()->getRenderSystem()->_cleanupDepthBuffers(false);
 
+  this->ogreViewport = nullptr;
   this->ogreTexture = nullptr;
 }

Edit: The patches causes other tests to fail because they're not calling renderTarget->Destroy();

@darksylinc
Copy link
Contributor

darksylinc commented Aug 12, 2022

OK here goes another attempt. Nothing crashes anymore, INTEGRATION_gpu_rays_ogre_gl3plus and INTEGRATION_lidar_visual_ogre_gl3plus fail; but they don't crash:

diff --git a/ogre/src/OgreCamera.cc b/ogre/src/OgreCamera.cc
index fafc217a..76ec9469 100644
--- a/ogre/src/OgreCamera.cc
+++ b/ogre/src/OgreCamera.cc
@@ -44,6 +44,12 @@ void OgreCamera::Destroy()
   if (!this->ogreCamera)
     return;
 
+  if (this->renderTexture)
+  {
+    this->renderTexture->Destroy();
+    this->renderTexture = nullptr;
+  }
+
   Ogre::SceneManager *ogreSceneManager;
   ogreSceneManager = this->scene->OgreSceneManager();
   if (ogreSceneManager == nullptr)
diff --git a/ogre/src/OgreDepthCamera.cc b/ogre/src/OgreDepthCamera.cc
index 0dd31cce..b6139696 100644
--- a/ogre/src/OgreDepthCamera.cc
+++ b/ogre/src/OgreDepthCamera.cc
@@ -109,12 +109,14 @@ void OgreDepthCamera::Destroy()
 
   if (this->dataPtr->pcdTexture)
   {
-    this->dataPtr->pcdTexture->RenderTarget()->removeAllViewports();
+    this->dataPtr->pcdTexture->Destroy();
+    this->dataPtr->pcdTexture.reset();
   }
 
   if (this->dataPtr->colorTexture)
   {
-    this->dataPtr->colorTexture->RenderTarget()->removeAllViewports();
+    this->dataPtr->colorTexture->Destroy();
+    this->dataPtr->colorTexture.reset();
   }
 
   if (!this->ogreCamera || !this->scene->IsInitialized())
diff --git a/ogre/src/OgreRenderTarget.cc b/ogre/src/OgreRenderTarget.cc
index d7ed64af..a170e52a 100644
--- a/ogre/src/OgreRenderTarget.cc
+++ b/ogre/src/OgreRenderTarget.cc
@@ -63,8 +63,7 @@ OgreRenderTarget::~OgreRenderTarget()
 {
   // TODO(anyone): clean up check null
 
-  OgreRTShaderSystem::Instance()->DetachViewport(this->ogreViewport,
-      this->scene);
+  GZ_ASSERT(this->ogreViewport == nullptr, "Destroy() not called!");
 }
 
 //////////////////////////////////////////////////
@@ -339,6 +338,8 @@ void OgreRenderTexture::DestroyTarget()
   if (nullptr == this->ogreTexture)
     return;
 
+  this->materialApplicator.reset();
+
   OgreRTShaderSystem::Instance()->DetachViewport(this->ogreViewport,
       this->scene);
 
@@ -351,6 +352,7 @@ void OgreRenderTexture::DestroyTarget()
   auto engine = OgreRenderEngine::Instance();
   engine->OgreRoot()->getRenderSystem()->_cleanupDepthBuffers(false);
 
+  this->ogreViewport = nullptr;
   this->ogreTexture = nullptr;
 }
 
diff --git a/ogre/src/OgreThermalCamera.cc b/ogre/src/OgreThermalCamera.cc
index 508c267c..19be0544 100644
--- a/ogre/src/OgreThermalCamera.cc
+++ b/ogre/src/OgreThermalCamera.cc
@@ -291,6 +291,12 @@ void OgreThermalCamera::Destroy()
     this->dataPtr->thermalImage = nullptr;
   }
 
+  if (this->dataPtr->thermalTexture)
+  {
+    this->dataPtr->thermalTexture->Destroy();
+    this->dataPtr->thermalTexture = nullptr;
+  }
+
   if (!this->ogreCamera || !this->scene->IsInitialized())
     return;
 
diff --git a/ogre/src/OgreWideAngleCamera.cc b/ogre/src/OgreWideAngleCamera.cc
index 860dd2c1..856e244e 100644
--- a/ogre/src/OgreWideAngleCamera.cc
+++ b/ogre/src/OgreWideAngleCamera.cc
@@ -156,6 +156,12 @@ void OgreWideAngleCamera::Destroy()
     this->dataPtr->wideAngleImage = nullptr;
   }
 
+  if (this->dataPtr->wideAngleTexture)
+  {
+    this->dataPtr->wideAngleTexture->Destroy();
+    this->dataPtr->wideAngleTexture = nullptr;
+  }
+
   for (unsigned int i = 0u; i < this->dataPtr->kEnvCameraCount; ++i)
   {
     if (this->dataPtr->envRenderTargets[i])

Co-authored-by: Matias N. Goldberg <[email protected]>
Signed-off-by: Michael Carroll <[email protected]>
@codecov
Copy link

codecov bot commented Aug 12, 2022

Codecov Report

❗ No coverage uploaded for pull request base (gz-rendering7@305f57b). Click here to learn what that means.
The diff coverage is n/a.

❗ Current head 3951589 differs from pull request most recent head 1bfcde2. Consider uploading reports for the commit 1bfcde2 to get more accurate results

@@               Coverage Diff                @@
##             gz-rendering7     #704   +/-   ##
================================================
  Coverage                 ?   74.18%           
================================================
  Files                    ?      159           
  Lines                    ?    14370           
  Branches                 ?        0           
================================================
  Hits                     ?    10661           
  Misses                   ?     3709           
  Partials                 ?        0           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@mjcarroll mjcarroll force-pushed the mjcarroll/ogre_depth_camera_segfault branch from 8425e98 to 845480d Compare August 16, 2022 21:21
Copy link
Contributor

@iche033 iche033 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the new test passes for me on macos and Focal

@@ -0,0 +1,241 @@
/*
* Copyright (C) 2017 Open Source Robotics Foundation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2022

@mjcarroll
Copy link
Contributor Author

@osrf-jenkins retest this please

@mjcarroll mjcarroll merged commit efc05f9 into gz-rendering7 Aug 18, 2022
@mjcarroll mjcarroll deleted the mjcarroll/ogre_depth_camera_segfault branch August 18, 2022 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working 🌱 garden Ignition Garden
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants